package com.moco.launcher.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.moco.launcher.R;
import com.oazon.analytics.AnalyticsManager;
import com.oazon.analytics.IAnalyticsCollector;
import com.oazon.analytics.UmengCollector;
import com.oazon.common.util.PowerKeyUtil;
import com.oazon.common.util.Utils;

/**
 * 主要为了接收电源按键的点击
 * @author Gavin
 *
 */
public class BaseFragmentActivity extends FragmentActivity {
	private static final String TAG = "BaseFragmentActivity";
	
	private PowerKeyUtil pkUtil = new PowerKeyUtil(this);

	private SharedPreferences mSharedPreferences;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!AnalyticsManager.getInsance().isInitialized()) {
            List<IAnalyticsCollector> collectors = new ArrayList<IAnalyticsCollector>();
            collectors.add(new UmengCollector());
            AnalyticsManager.getInsance().init(BaseFragmentActivity.this, collectors);
        }
        mSharedPreferences = getSharedPreferences(Utils.CONFIG_FILE, MODE_PRIVATE);
    }


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		//pkUtil.powerKeyDown(keyCode, event);
		
		return super.onKeyDown(keyCode, event);
	}
    
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		
		if(keyCode==26){
		pkUtil.SetSysNomalOrProtect(keyCode, event);
		}
		return super.onKeyUp(keyCode, event);
	}
	//add by vincent 150610
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
        int keyCode=event.getKeyCode();
        //Log.e("start",String.valueOf(start));
        switch(keyCode)
        {
            case 26:
            	//event.getAction()
            if(event.isLongPress())   
              {
            	if(event.getAction()==event.ACTION_UP){
            		
            	}else{
            		pkUtil.askForPowrOff(keyCode, event);
            	}
            	//event.getAction()==
            	
                return true;
            }
            //return super.dispatchKeyEvent(event);  //如果不是长按，则调用原有方法，执行按下back键应有的处理
            default:
                break;
        }

		return super.dispatchKeyEvent(event);
	}


	@Override
	public void onResume() {
	    super.onResume();
	    
	    AnalyticsManager.getInsance().onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
	    
	    AnalyticsManager.getInsance().onPause(this);
	}
	
	public void addGuideImage(int id){
		
		View view = getWindow().getDecorView().findViewById(R.id.main_layout);
		if(view==null){
			return;
		}
		if(mSharedPreferences.getBoolean("mFirst", false)){
			return;
		}
		ViewParent viewParent = view.getParent();
		if(viewParent instanceof FrameLayout){
			final FrameLayout frameLayout = (FrameLayout) viewParent;
			if(id!=0){
				 final ImageView mguideImage = new ImageView(this);
				FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				params.setMargins(0, 150, 0, 10);
				mguideImage.setLayoutParams(params);
				mguideImage.setScaleType(ScaleType.FIT_CENTER);
				mguideImage.setImageResource(id);
				mguideImage.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						mguideImage.setVisibility(View.GONE);
					}
				});
				mguideImage.setVisibility(View.VISIBLE);
				frameLayout.addView(mguideImage);
			}
		}
		
	}
}
